<template>
  <div>
    <el-dialog
      title="修改金额"
      center
      :visible.sync="isShow"
      width="30"
      append-to-body
      :close-on-click-modal="false"
      @close="closeDialog"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="修改金额：" prop="money">
          <el-input v-model.number="form.money "></el-input>
        </el-form-item>

        <el-form-item label="修改原因：" prop="reason">
          <el-input type="textarea" v-model="form.reason"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    var validateMoney = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入提现金额"));
      } else {
        if (!Number(value)) {
          callback(new Error("请输入正确的金额"));
        } else if (value <= 0) {
          callback(new Error("提现金额必须大于0"));
        } else {
          callback();
        }
      }
    };
    return {
      isShow: false,
      form: {
        money: null,
        reason: ""
      },
      agent_id: null,
      finals_id: null,
      order_id: null,
      settlement_id: null,
      total: null,
      rules: {
        money: [{ validator: validateMoney, trigger: "blur" }],
        reason: [{ required: true, message: "原因", trigger: "blur" }]
      }
    };
  },
  methods: {
    show(data) {
      console.log(data)
      this.agent_id = data.agent_id;
      this.finals_id = data.finals_id;
      this.order_id = data.order_id;
      this.settlement_id = data.settlement_id;
      this.total = data.total;
      this.isShow = true;
    },
    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {

          console.log(this.total)
          console.log(this.form.money)
          if (this.form.money > this.total) {
            this.submitReward();
          } else {
            this.submitPunishment();
          }
        } else {
          return false;
        }
      });
    },
    submitReward() {
      let params = {
        agent_id: this.agent_id,
        finals_id: this.finals_id,
        reason: this.form.reason,
        reward: this.form.money
      };
      this.$axios.post("/api/rewardRecord", params).then(res => {
        if (res.data.code == 200) {
          this.$message({
            type: "success",
            message: "操作成功"
          });
          this.$emit("refresh");
          this.isShow = false;
        } else {
          this.$message({
            type: "error",
            message: res.data.message
          });
        }
      });
    },
    submitPunishment() {
      let params = {
        settlement_id: this.settlement_id,
        order_id: this.order_id,
        agent_id: this.agent_id,
        deduction_price: this.form.money,
        name: this.form.reason
      };
      this.$axios.post("/api/deduction", params).then(res => {
        if (res.data.code == 200) {
          this.$message({
            type: "success",
            message: "操作成功"
          });
          this.$emit("refresh");
          this.isShow = false;
        } else {
          this.$message({
            type: "error",
            message: res.data.message
          });
        }
      });
    },
    closeDialog() {
      this.$refs["ruleForm"].resetFields();
      this.isShow = false;
    }
  }
};
</script>

<style lang='less' type='text/css'>
</style>
